package siddur.traktask.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import siddur.traktask.entity.TaskInfo;
import siddur.traktask.entity.TrakPeriod;

public interface TrakPeriodRepository extends CrudRepository<TrakPeriod, Long>{

	List<TrakPeriod> findByStartAtAndTask(Date startAt, TaskInfo task);
	
	@Query("select t from TrakPeriod t where t.task = :task and ((t.startAt between :startAt and :endAt) or (t.endAt between :startAt and :endAt) or (t.startAt < :startAt and t.endAt > :endAt)) ")
	List<TrakPeriod> findBetween(@Param("startAt") Date startAt, 
			@Param("endAt") Date endAt, 
			@Param("task") TaskInfo task);
	
	@Query("select sum(t.seconds) from TrakPeriod t where t.task.id = :taskId and t.startAt >= :startAt")
	Long getSumOfSeconds(@Param("startAt") Date startAt, @Param("taskId") Long taskId);
	
	@Query("select sum(t.seconds) from TrakPeriod t where t.task.id = :taskId and t.startAt >= :startAt and t.endAt < :endAt")
	Long getSumOfSeconds(@Param("startAt") Date startAt, @Param("endAt") Date endAt, @Param("taskId") Long taskId);
}
